package com.lijiankun24.shadowlayoutexample.provider;

import com.lijiankun24.shadowlayoutexample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import java.util.List;

/**
 * 列表适配器
 *
 * @author lijiankun
 * @since 2021-03-11
 */
public class SampleItemProvider extends BaseItemProvider {
    private List<String> list;
    private AbilitySlice abilitySlice;

    /**
     * 构造方法
     *
     * @param list
     * @param abilitySlice
     */
    public SampleItemProvider(List<String> list, AbilitySlice abilitySlice) {
        this.list = list;
        this.abilitySlice = abilitySlice;
    }

    /**
     * getCount
     *
     * @return int
     */
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    /**
     * getItem
     *
     * @param position
     * @return Object
     */
    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()) {
            return list.get(position);
        }
        return new Object();
    }

    /**
     * getItemId
     *
     * @param position
     * @return long
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * getComponent
     *
     * @param position
     * @param component
     * @param componentContainer
     * @return Component
     */
    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Component component1 = LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_item_sample,
                null, false);

        String string = list.get(position);
        Text text = (Text) component1.findComponentById(ResourceTable.Id_text_item);
        text.setText(string);

        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
            }
        });

        return component1;
    }
}
